package com.lzwork.gpt.utility.thread;

import java.util.HashMap;
import java.util.Map;

import com.lzwork.utility.logger.LOGGER;

/**
 * @author LiZheng
 * @date 2024年3月14日 15:50:05
 */

public abstract class MultiCaller
{
	protected Map<String, CallerThread> callerMap = new HashMap<>();

	public enum RunState
	{
		Idle,
		Work,
		Stop
	}

	protected RunState localState = RunState.Idle;

	protected abstract CallerThread createJob(String server);

	public abstract void doJob(CallerThread caller);

	public boolean addServer(String url)
	{
		if (callerMap.containsKey(url))
		{
			LOGGER.log(LOGGER.NET, "Duplicated server: " + url);
			return false;
		}

		CallerThread caller = createJob(url);
		callerMap.put(url, caller);

		return true;
	}

	public void onCallerInterupt(CallerThread caller)
	{
	}

	public RunState state()
	{
		return localState;
	}

	public void start()
	{
		for (CallerThread caller : callerMap.values())
		{
			caller.start();
		}

		localState = RunState.Work;
	}

	public void stop()
	{
		localState = RunState.Stop;
	}
}
